//
//  LinkingCardLoginView.swift
//  iPhone
//
//  Created by Toj on 11/9/22.
//

import UIKit

class LinkingCardLoginView: UIView {
    
    var phone: String = "" { didSet {
        let title = "Your phone number " + phone + " \nhas been tied with smart card, please click the button to log in and enjoy the premium content"
        let (attributedText, _) = title.to_attributedString(font: UIFont.systemFont(ofSize: 14.f_scale), color: phoneLabel.textColor, highlight: phone, hfont: UIFont.boldSystemFont(ofSize: 14.f_scale), hcolor: phoneLabel.textColor)
        phoneLabel.attributedText = attributedText
        setNeedsLayout()
    } }
    
    var decoder: String = "" { didSet {
        var attributedText: NSMutableAttributedString?
        if !decoder.isEmpty {
            let title = "The account password is the last six digits of the decoder: " + decoder
            let (attributedString, _) = title.to_attributedString(font: UIFont.systemFont(ofSize: 11.f_scale), color: phoneLabel.textColor, highlight: decoder, hfont: UIFont.boldSystemFont(ofSize: 11.f_scale), hcolor: "#FF0000".to_color)
            attributedText = attributedString
        }
        decoderLabel.attributedText = attributedText
        setNeedsLayout()
    } }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupSubviews()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        setupLayoutSubviews()
    }
    
    private(set) lazy var phoneLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textAlignment = .center
        view.textColor = "#666666".to_color
        view.font = UIFont.systemFont(ofSize: 14.f_scale)
        return view
    }()
    
    private(set) lazy var decoderLabel: UILabel = {
        let view = UILabel()
        view.numberOfLines = 0
        view.textAlignment = .center
        view.textColor = "#666666".to_color
        view.font = UIFont.systemFont(ofSize: 11.f_scale)
        return view
    }()
    
    private(set) lazy var notLoginButton: UIButton = {
        let view = UIButton()
        let attributedString = "Do not log in for now".lg_localized.to_attributedString(font: UIFont.systemFont(ofSize: 12.f_scale), color: "#AAAAAA".to_color, lineStyle: .underline)
        view.setAttributedTitle(attributedString, for: .normal)
        return view
    }()
    
    private let loginButtonHeight = 32.ui_scale
    private(set) lazy var loginButton: UIButton = {
        let view = UIButton()
        view.backgroundColor = "#E8CD8C".to_color
        view.setTitle("LOG IN FOR FREE", for: .normal)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.titleLabel?.font = UIFont.boldSystemFont(ofSize: 14.f_scale)
        view.cm_cornerRadius = loginButtonHeight * 0.5
        return view
    }()
    
    deinit {
        // print("-->toj: LinkingCardLoginView " + #function)
    }
}

extension LinkingCardLoginView {
    
    private func setupSubviews() {
        
        addSubview(phoneLabel)
        addSubview(decoderLabel)
        addSubview(notLoginButton)
        addSubview(loginButton)
    }
    
    private func setupLayoutSubviews() {
        
        phoneLabel.snp.makeConstraints { make in
            make.top.equalTo(24)
            make.left.right.equalTo(self).inset(16.ui_scale)
        }
        
        decoderLabel.snp.makeConstraints { make in
            make.top.equalTo(phoneLabel.snp.bottom).offset(12.ui_scale)
            make.left.right.equalTo(self).inset(16.ui_scale)
        }
        
        loginButton.snp.makeConstraints { make in
            make.height.equalTo(loginButtonHeight)
            make.bottom.equalTo(snp.bottom).offset(-40.ui_scale)
            make.left.right.equalTo(self).inset(16.ui_scale)
        }
        
        notLoginButton.snp.makeConstraints { make in
            make.height.equalTo(loginButtonHeight)
            make.bottom.equalTo(loginButton.snp.top).offset(-20.ui_scale)
            make.left.right.equalTo(self).inset(16.ui_scale)
        }
    }
}
